package com.ncloud.resman.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ncloud.resman.entity.Model;

@Mapper
public interface ModelMapper {

	@Select("select a.brand_id as brandId,b.brand_name as brandName,model_id modelId,model_name modelName,a.eqpt_type eqptType,eqpt_spec eqptSpec,"
			+ "(select dict_value from sys_dictionary where dictionary_id = 'eqpt_type' and dict_key = a.eqpt_type) as eqptTypeName"
			+ " from res_base_model a,res_base_brand b where a.eqpt_type=ifnull(#{eqptType},a.eqpt_type)"
			+ " and a.brand_id=ifnull(#{brandId},a.brand_id) and a.state ='1' and a.brand_id = b.brand_id")
	List<Map<String,Object>> findModelsByEqptTypeAndBrandId(@Param("eqptType")String eqptType, @Param("brandId")Long brandId);

	@Select("select model_id,eqpt_type,brand_id,model_name,eqpt_spec from res_base_model where model_id = #{modelId} and state ='1'")
	@Results({@Result(column="model_id",property="modelId"),@Result(column="eqpt_type",property="eqptType"),
		@Result(column="brand_id",property="brandId"),@Result(column="model_name",property="modelName"),
		@Result(column="eqpt_spec",property="eqptSpec")})
	Model findModelById(Long modelId);

	@Insert("insert into res_base_model(model_id, eqpt_type, brand_id, model_name, eqpt_spec,"
			+ " state, create_user, create_date, update_user, update_date, remarks)"
			+ " values(#{modelId}, #{eqptType}, #{brandId}, #{modelName}, #{eqptSpec},"
			+ " #{state}, #{createUser}, #{createDate}, #{updateUser}, #{updateDate}, #{remarks})")
	int insertModel(Model model);

	@Update("update res_base_model set state = #{state},update_date=#{updateDate},update_user = #{updateUser}"
			+ " where model_id = #{modelId} and state = '1'")
	int deleteModel(Model model);

	@Update("update res_base_model set brand_id =#{brandId},model_name=#{modelName},eqpt_spec=#{eqptSpec},"
			+ " update_user=#{updateUser},update_date=#{updateDate} where model_id=#{modelId} and state ='1'")
	int updateModel(Model model);

	@Select("select model_id,eqpt_type,brand_id,model_name,eqpt_spec"
			+ " from res_base_model where eqpt_type = #{eqptType} and brand_id = #{brandId} and model_name = #{modelName}")
	@Results({@Result(column="model_id",property="modelId"),@Result(column="eqpt_type",property="eqptType"),
		@Result(column="brand_id",property="brandId"),@Result(column="model_name",property="modelName"),
		@Result(column="eqpt_spec",property="eqptSpec")})
	Model getModelByModelName(@Param("eqptType")String eqptType, @Param("brandId")Long brandId, @Param("modelName")String modelName);
	
	@Select("select model_id,eqpt_type,brand_id,model_name,eqpt_spec from res_base_model where model_id = #{modelId}")
	@Results({@Result(column="model_id",property="modelId"),@Result(column="eqpt_type",property="eqptType"),
		@Result(column="brand_id",property="brandId"),@Result(column="model_name",property="modelName"),
		@Result(column="eqpt_spec",property="eqptSpec")})
	Model getModelInfoById(Long modelId);
	
	@Select("select model_id,eqpt_type,brand_id,model_name,eqpt_spec,state from res_base_model where model_name = #{0}")
	@Results({@Result(column="model_id",property="modelId"),@Result(column="eqpt_type",property="eqptType"),
		@Result(column="brand_id",property="brandId"),@Result(column="model_name",property="modelName"),
		@Result(column="state",property="state"),@Result(column="eqpt_spec",property="eqptSpec")})
	Model getModelInfoByName(String  modelName);
	
}
